<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>用户列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片区域 -->
        <el-card class="box-card">
            <!-- 搜索条件框 -->
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="clearCondition">
                      <el-button slot="append" icon="el-icon-search" @click="getUserList">
                      </el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button  type="primary" @click="addUserDialogVisible=true">添加</el-button>
                </el-col>
            </el-row>
            <!-- 表格数据 -->
            <el-table :data="usersList" style="width: 100%" stripe>
                <!-- 索引列 -->
                <el-table-column type="index" label="序号" width="120"></el-table-column>
                <el-table-column prop="username" label="姓名"></el-table-column>
                <el-table-column prop="email" label="邮箱"></el-table-column>
                <el-table-column prop="mobile" label="电话"></el-table-column>
                <el-table-column prop="role_name" label="角色"></el-table-column>
                <!-- prop="mg_state" -->
                <el-table-column  label="状态"> 
                    <template slot-scope="sc">
                        <el-switch v-model="sc.row.mg_state" @change="changeUserState(sc.row)">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column  label="操作" width="180">
                    <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
                    <el-button type="danger" icon="el-icon-delete" size="mini"></el-button>
                    <el-tooltip  effect="dark" content="权限分配" placement="top-start" :enterable="false"  >
                      <el-button type="warning" icon="el-icon-setting" size="mini"></el-button>
                    </el-tooltip>
                </el-table-column>
            </el-table>
             <!-- 分页区域 -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[1, 2, 4, 6]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </el-card>
   <!-- 新增用户对话框 -->
  <el-dialog  title="添加用户" :visible.sync="addUserDialogVisible" width="50%">
        <el-form 
        :model="addUserForm" 
        ref="addUserFormRef" 
        :rules="addUserFormRules" 
        label-width="100px"
        >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addUserForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addUserForm.password"></el-input>
        </el-form-item>
         <el-form-item label="邮箱" prop="email">
          <el-input v-model="addUserForm.email"></el-input>
        </el-form-item>
         <el-form-item label="电话" prop="mobile">
          <el-input v-model="addUserForm.mobile"></el-input>
        </el-form-item>
      </el-form>
            <!-- 确定/取消 按钮 -->
    <span slot="footer" class="dialog-footer">
        <el-button @click="addUserDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUserDialogVisible = false">确 定</el-button>
    </span>
</el-dialog>


    </div>
</template>

<script>
export default {
  data () {
    return {
      // 添加用户参数  
      addUserForm: {
        username: '',
        password: '',
        email: '',
        mobile: ''
      },
      // 添加用户参数校验规则
      addUserFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '请输入电话', trigger: ['blur', 'change'] },
          {validator:function(rule,value,callback){
                // https://www.cnblogs.com/duanxz/p/4754036.html 
                var myreg = /^1[34578]\d{9}$/
                var s1=/^1(3[4-9]|5[012789]|8[78])\d{8}$/
	            if(!myreg.test(value)){
	                callback(new Error('请输入正确的手机号'))
                } else if(!s1.test(value)){
	                callback(new Error('请输入移动手机号'))
                }else{
	                callback()
	            }
          }, trigger: ['blur', 'change']}
        ]
      },
     // 获取列表用户的条件参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 2
      },
      pagenum: '',
       // 分页组件里面的total数据类型是Number不是String，所以用0初始化
      total: 0,
      usersList: [],
      addUserDialogVisible: false
    }
  },
  created () {
     this.getUserList()
  },
  methods: {
    //方法1
    async  getUserList() {
    // data属性赋值给res    
    const { data: res } =  await this.$http.get('users',{ params:this.queryInfo })
    console.log(res)
    if(res.meta.status !== 200){
        return this.$message.error('获取列表失败')
    }
    this.usersList=res.data.users
    this.pagenum=res.data.pagenum
    // 分页组件里面的total数据类型是Number不是String
    this.total=parseInt(res.data.total)
   },
   //方法2 
      async clearCondition() {
    // data属性赋值给res
    this.queryInfo.query=null    
    const { data: res } =  await this.$http.get('users',{ params:this.queryInfo })
    console.log(res)
    if(res.meta.status !== 200){
        return this.$message.error('获取列表失败')
    }
    this.usersList=res.data.users
    this.pagenum=res.data.pagenum
    // 分页组件里面的total数据类型是Number不是String
    this.total=parseInt(res.data.total)
   },
   handleSizeChange(pageSize){
       this.queryInfo.pagesize=pageSize
       this.getUserList()
   },
     handleCurrentChange(pageNumber){
       this.queryInfo.pagenum=pageNumber
       this.getUserList()
   },
  async changeUserState(userInfo){
   const { data: res } = 
    //这里有个反引号``进行参数解析  
   await this.$http.put(`users/${userInfo.id}/state/${userInfo.mg_state}`)
   console.log(res)
   if(res.meta.status !==200){
    //让上面的数据还原
     userInfo.mg_state=!userInfo.mg_state
     return  this.$message.error('更新用户状态失败')
   }
   this.$message.success('更新用户状态成功')
   }
  }
}
</script>

<style lang="less" scoped>

</style>
